package com.penngo.gui.dfdiff.view;

import com.penngo.gui.dfdiff.utils.GB;
import com.penngo.gui.dfdiff.utils.ToolUtil;
import org.apache.pdfbox.multipdf.PDFMergerUtility;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class MergePdfDialog extends BaseDialog{

    private JTextField mergePath;
    private JTextField pdfFile;
    private JFileChooser folderChooser;
    private JFileChooser fileChooser;
    public MergePdfDialog() {
        System.out.println("MergePdfDialog==========");
//        setSize(400, 200);
    }

    @Override
    public void initUI() {
        setSize(600, 200);
        JLabel pathLabel = new JLabel("需要合并PDF路径：");
        this.add(pathLabel, new GB(0,0,1,1).setInsets(10));
        mergePath = new JTextField();
        this.add(mergePath, new GB(1,0,1,1).setFill(GB.BOTH).setWeight(100, 0).setInsets(10));
        JButton openBtn = new JButton("选择");
        openBtn.addActionListener(e -> openFolder());
        this.add(openBtn, new GB(2,0,1,1).setInsets(10));

        JLabel pdfLabel = new JLabel("合并后文的件路径：");
        this.add(pdfLabel, new GB(0,1,1,1).setInsets(10));
        pdfFile = new JTextField();
        this.add(pdfFile, new GB(1,1,1,1).setFill(GB.BOTH).setWeight(100, 0).setInsets(10));
        JButton fileBtn = new JButton("选择");
        fileBtn.addActionListener(e -> savePdfName());
        this.add(fileBtn, new GB(2,1,1,1).setInsets(10));

        JButton saveBtn = new JButton("开始合并");
        saveBtn.addActionListener(e -> mergePdf());
        this.add(saveBtn, new GB(0,2,2,1).setInsets(10));
    }

    private void openFolder(){
        if(folderChooser == null){
            folderChooser = new JFileChooser(".");
            folderChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        }

        int result = folderChooser.showOpenDialog(null);
        if(result == JFileChooser.APPROVE_OPTION){
            File selectFile = folderChooser.getSelectedFile();
            if(selectFile.isFile()){
                selectFile = selectFile.getParentFile();
            }
            //node = new ProjectNode(selectFile.getName(), selectFile,null, ProjectNode.ProjectType.FOLDER);
            mergePath.setText(selectFile.getAbsolutePath());
            String saveFile = selectFile.getParentFile().getAbsolutePath()  + File.separatorChar + "merge.pdf";
            pdfFile.setText(saveFile);
        }
    }

    private void mergePdf(){
        try{
            File f = new File(pdfFile.getText());
            if(f.exists()){
                int yes_ok = JOptionPane.showConfirmDialog(null, "文件已存在，是否覆盖？", "提示", JOptionPane.YES_NO_OPTION);
                if(yes_ok == JOptionPane.YES_OPTION){
                    merge();
                }
            }
            else{
                merge();
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

    private void merge() throws Exception{
        PDFMergerUtility pdfMerger = new PDFMergerUtility();
        File file = new File(mergePath.getText());
        for (File f : file.listFiles()) {
            String name = f.getName().toLowerCase();
            if (name.endsWith(".pdf")) {
                pdfMerger.addSource(f);
            }

        }
        pdfMerger.setDestinationFileName(pdfFile.getText());
        pdfMerger.mergeDocuments(null);
        JOptionPane.showMessageDialog(null, "合并完成");
    }
    private void savePdfName(){
        if(fileChooser == null){
            fileChooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter(
                    "PDF(*.pdf)", "pdf");
            fileChooser.addChoosableFileFilter(filter);
        }
        fileChooser.setSelectedFile(new File(mergePath.getText()));
        int returnVal = fileChooser.showSaveDialog(null);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            File f = fileChooser.getSelectedFile();
            String fullname = fileChooser.getCurrentDirectory().getAbsolutePath() + File.separatorChar + f.getName();
            pdfFile.setText(fullname);





        }
    }
    public static void main(String[] args) {
        MergePdfDialog dialog = new MergePdfDialog();
        dialog.setVisible(true);
    }

}
